package com.gzyj.video.server;

import com.gzyj.video.config.MyFtpPlet;
import lombok.extern.slf4j.Slf4j;
import org.apache.ftpserver.DataConnectionConfigurationFactory;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.ftplet.DefaultFtplet;
import org.apache.ftpserver.ftplet.Ftplet;
import org.apache.ftpserver.listener.Listener;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration("FTPServer")
@Slf4j
public class FTPServer extends CachingConfigurerSupport {


    @Value("${ftp.server.port}")
    private Integer port;
    @Value("${ftp.server.savePath}")
    private String savePath;
    @Autowired
    private DataSource dataSource;


    public FTPServer(DataSource dataSource) {
        this.dataSource = dataSource;

    }

    @Bean
    public DefaultFtplet createFtplet(){
        return new MyFtpPlet();
    }

    @Autowired
    private MyFtpPlet myFtpPlet;

    @Bean
    public FtpServer createFtpServer() {
        FtpServerFactory serverFactory = new FtpServerFactory();
        ListenerFactory listenerFactory = new ListenerFactory();

        //设置监听端口
        listenerFactory.setPort(port);

        //2、设置被动模式数据上传的接口范围,云服务器需要开放对应区间的端口给客户端
        DataConnectionConfigurationFactory dataConnectionConfFactory = new DataConnectionConfigurationFactory();
        dataConnectionConfFactory.setPassivePorts("10000-10500");
        listenerFactory.setDataConnectionConfiguration(dataConnectionConfFactory.createDataConnectionConfiguration());
        Listener listener = listenerFactory.createListener();
        //替换默认监听

        serverFactory.addListener("default", listener);


        //用户名
        BaseUser user = new BaseUser();
        user.setName("admin");
        //密码 如果不设置密码就是匿名用户
        user.setPassword("123456");
        //用户主目录
        user.setHomeDirectory(savePath);

        List<Authority> authorities = new ArrayList<Authority>();
        //增加写权限
        authorities.add(new WritePermission());
        user.setAuthorities(authorities);

        //增加该用户
        try {
            serverFactory.getUserManager().save(user);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //5、配置自定义用户事件
        Map<String, Ftplet> ftpLets = new HashMap<String, Ftplet>();
        ftpLets.put("miaFtplet", myFtpPlet);
        serverFactory.setFtplets(ftpLets);
        return serverFactory.createServer();

    }



}
